package com.feng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.constants.SystemConstants;
import com.feng.domain.ResponseResult;
import com.feng.domain.entity.Menu;
import com.feng.domain.vo.MenuListVo;
import com.feng.domain.vo.MenuVo;
import com.feng.mapper.MenuMapper;
import com.feng.service.MenuService;
import com.feng.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author makejava
 * @since 2022-10-15 11:23:12
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<String> selectPermsByUserId(Long userId) {
        //判断是否是管理员,如果是管理员就将权限信息全部返回
        if(userId.equals(1L)&&userId!=null){
            LambdaQueryWrapper<Menu> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.in(Menu::getMenuType,SystemConstants.MENU,SystemConstants.BUTTON);

            queryWrapper.eq(Menu::getStatus,SystemConstants.STATUESS);

            List<Menu> list = this.list(queryWrapper);
            List<String> perms = list.stream().map(Menu::getPerms)
                    .collect(Collectors.toList());
            return perms;
        }

       return menuMapper.selectPermsByUserId(userId);

    }

    @Override
    public List<MenuVo> getRouterMenuTreeByUserId(Long userId) {
        List<Menu> menu=null;
        //判断是否是管理员
        if(userId.equals(1L)&&userId!=null){
            //如果是 返回所有符合要求的menus
             menu= menuMapper.selectAllRouterMenus();
            List<MenuVo> menuVos = BeanCopyUtils.copyBeanList(menu, MenuVo.class);
                List<MenuVo> list=buildMenuTree(menuVos,0L);
            return list;
            //构建tree
        }

        //否则 返回当前用户所具有的menus
        else {
           menu=menuMapper.selectRouterMenuTreeByUserId(userId);
            //构建tree
            List<MenuVo> menuVos = BeanCopyUtils.copyBeanList(menu, MenuVo.class);
            List<MenuVo> list=buildMenuTree(menuVos,0L);
            return list;
        }
    }

    @Override
    public ResponseResult getMenuList(String status, String menuName) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        //menuName模糊查询
        queryWrapper.like(StringUtils.hasText(menuName),Menu::getMenuName,menuName);
        queryWrapper.eq(StringUtils.hasText(status),Menu::getStatus,status);
        //排序 parent_id和order_num
        queryWrapper.orderByAsc(Menu::getParentId,Menu::getOrderNum);
        List<Menu> menus = list(queryWrapper);
        List<MenuListVo> menuListVos = BeanCopyUtils.copyBeanList(menus, MenuListVo.class);
        return ResponseResult.okResult(menuListVos);
    }

    @Override
    public boolean hasChild(Long menuId) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId,menuId);
        return count(queryWrapper) != 0;
    }

    @Override
    public ResponseResult TreeSelectRole() {
        return null;
    }

    private List<MenuVo> buildMenuTree(List<MenuVo> menuVos,Long parentId) {
        //先找第一层的菜单，然后去找他们的子菜单设置到chilrdern
        List<MenuVo> menuTree = menuVos.stream()
                //过滤出1级目录
                .filter(m -> m.getParentId().equals(parentId))
                //m为一级目录的单个目录，为其设置对应的children
                .map(m -> m.setChildren(getChildren(m, menuVos)))
                .collect(Collectors.toList());
        return menuTree;
    }

    /**
     * 获取传入参数的 子菜单
     * @param m  为当前一级目录
     * @return
     */
    private List<MenuVo> getChildren(MenuVo m,List<MenuVo> menuVos) {
        List<MenuVo> ChildrenList = menuVos.stream().filter(v -> v.getParentId().equals(m.getId()))
                .map(v->{
                    v.setChildren(getChildren(v,menuVos));

                    return v;
                })
                .collect(Collectors.toList());

        return ChildrenList;
    }
}

